package com.example.bjsfpost.utils;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.example.bjsfpost.emails.BjsfEmailResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class JwtUtils {
    public static final String APP = "appName";
    public static void verifyToken(String token,String secret) throws TokenExpiredException, SignatureVerificationException, JWTDecodeException {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build();
        verifier.verify(token);
    }
    public static Claim getClaimByName(String token,String name){
        return JWT.decode(token).getClaim(name);
    }


    public static String apiCreateAppToken(String secret, String appName){
        Calendar nowTime = Calendar.getInstance();
        // 设置24小时过期时间
//        nowTime.add(Calendar.HOUR,24);
        nowTime.add(Calendar.SECOND,10);
        Date expiresDate = nowTime.getTime();
        return JWT.create()
                .withExpiresAt(expiresDate)
                .withClaim(APP, appName)
                .sign(Algorithm.HMAC256(secret));
    }


    public static void main(String[] args) {

        System.out.println(apiCreateAppToken("26AA2AB0F40D4C0BB5EA739E281471EF", "MPM"));
    }



}
